import axios from 'axios';
import {ResponseBody} from "./ResponseBody";
import {message} from "antd";

class HttpClient implements HttpClientInterface{

  post(url :string,data,call):ResponseBody<any>{
    let body = new ResponseBody();
    axios.post(url, data)
      .then(function (response) {
        if(response.data ==undefined){
          body.code ="999999";
          body.message="服务端返回异常";
          call(body,false);
        }else{
          body.code =response.data.code;
          body.message=response.data.message;
          body.data = response.data.data;
          call(body,true,data);
        }
      })
      .catch(function (error) {
        console.log(error);
        body.code ="999998";
        body.message="服务端调用异常";
        call(body,false);
      });
    return body;
  };
  get(url :string,data,call):ResponseBody<any>{
    let body = new ResponseBody();
    axios.get(url, data)
      .then(function (response) {
        if(response.data ==undefined){
          body.code ="999999";
          body.message="服务端返回异常";
          call(body,false);
        }else{
          body.code =response.data.code;
          body.message=response.data.message;
          body.data = response.data.data;
          call(body,true,data);
        }
      })
      .catch(function (error) {
        console.log(error);
        body.code ="999998";
        body.message="服务端调用异常";
        call(body,false);
      });
    return body;
  };
  postAndToken(url :string,data,token:string,call):ResponseBody<any>{
    let body = new ResponseBody();
    axios({
      method:"post",
      url:url,
      data:data,
      headers:{
        "Authorization":"Bearer " + token
      }
    }).then(function (response) {
      if(response.data ==undefined){
        body.code ="999999";
        body.message="服务端返回异常";
        call(body,false);
      }else{
        body.code =response.data.code;
        body.message=response.data.message;
        body.data = response.data.data;
        call(body,true,data);
      }
    })
      .catch(function (error) {
        console.log(error);
        body.code ="999998";
        body.message="服务端调用异常";
        call(body,false);
      });
    return body;
  };
  getAndToken(url :string,data,token:string,call):ResponseBody<any>{
    let body = new ResponseBody();
    axios({
      method:"get",
      url:url,
      params:data,
      headers:{
        "Authorization":"Bearer " + token
      }
    }).then(function (response) {
      if(response.data ==undefined){
        body.code ="999999";
        body.message="服务端返回异常";
        call(body,false);
      }else{
        body.code =response.data.code;
        body.message=response.data.message;
        body.data = response.data.data;
        call(body,true,data);
      }
    }).catch(function (error) {
        console.log(error);
        body.code ="999998";
        body.message="服务端调用异常";
        call(body,false);
      });
    return body;
  };

  sendFIle<T>(url: string,token:string, formDate, call): ResponseBody<T> {
    let body = new ResponseBody();
    axios({
      method:"put",
      url:process.env.POINT_SHOPPING_URL+url,
      data:formDate,
      headers:{
        "Authorization":"Bearer " + token
      }
    }).then(function (response) {
      if(response.data ==undefined){
        body.code ="999999";
        body.message="服务端返回异常";
        call(body,false);
      }else{
        body.code =response.data.code;
        body.message=response.data.message;
        body.data = response.data.data;
        call(body,true,undefined);
      }
    })
      .catch(function (error) {
        console.log(error);
        body.code ="999998";
        body.message="服务端调用异常";
        call(body,false);
      });
    return null;
  }

}

interface HttpClientInterface {

  post<T>(url :string,data,call):ResponseBody<T>;

  sendFIle<T>(url :string,token:string,formDate,call):ResponseBody<T>;
}

export default HttpClient;
